package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StatisVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Override
    public String getTagsByDetailId(Long detailId) {
        return strategyDetailMapper.getTagsByDetailId(detailId);
    }

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyContent getContentByDetailId(Long detailId) {
        return strategyContentMapper.getContentByDetailId(detailId);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //1:设置目的地
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());
        Destination dest = strategy.getDest();
        strategyDetail.setDest(dest);

        //2:作者:目前处理

        //3:摘要:
        //获取摘要的方案
        //(1):攻略文章作者自己提取(推荐)
        //(2):截取,文章内容前200字符
        String strategyContent = strategyDetail.getStrategyContent().getContent();
        if(strategyContent.length() > 200){
            strategyDetail.setSummary(strategyContent.substring(0,200));
        }else {
            strategyDetail.setSummary(strategyContent);
        }

        //5:是否是国外
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        strategyDetail.setIsabroad(toasts.get(0).getId() != 1L);

        //6:内容的id设置,整个新增或修改
        StrategyContent content= strategyDetail.getStrategyContent();
        if(strategyDetail.getId() == null){
            //创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);

            content.setId(strategyDetail.getId());
            strategyContentMapper.insert(content);
        }else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            content.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(content);
        }

        /**
         * 标签处理
         */
        //为了在编辑时,避免重复添加,先进行删除操作
        strategyDetailMapper.deleteDetailTagRelation(strategyDetail.getId());
        if(StringUtils.hasLength(tags)){
            String[] tagString = tags.split(",");
            if(tagString != null && tagString.length>0){
                for (String s : tagString) {
                    StrategyTag strategyTag = new StrategyTag();
                    strategyTag.setName(s);
                    strategyTagService.saveOrUpdate(strategyTag);
                    //维护与明细多对多的关系
                    strategyDetailMapper.insertTagDetailRelation(strategyTag.getId(),strategyDetail.getId());
                }
            }
        }
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getViewnumTop3(Long destId) {
        return strategyDetailMapper.selectViewnumTop3(destId);
    }

    @Override
    public void updateStrategyStatisVo(StatisVo statisVo) {
        strategyDetailMapper.updateStrategyStatisVo(statisVo);
    }
}
